﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Imenik_Projektni_2011
{
    public partial class frmLogin : Form
    {
        OleDbConnection conn = new OleDbConnection();
        DataSet ds = null;
        OleDbDataAdapter da = null;
        int id = 0;
        

        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            conn.ConnectionString = @"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=..\..\Baza\Imenik.accdb";
            OleDbCommand cmd = new OleDbCommand("select * from users", conn);

            da = new OleDbDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds);
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            DataTable tbl = ds.Tables[0];
            
            bool checkPassword = false;

            foreach (DataRow slog in tbl.Rows)
            {
                if (txtUsername.Text == slog["username"].ToString()) //Ako se podataka iz txtUsername slaže sa podatkom iz baze
                {
                    MessageBox.Show("Nalog je ok");
                    id = Convert.ToInt32(slog["id"]);

                    if (txtPassword.Text == slog["password"].ToString()) // Proveri da li se txtpassword slaže sa passwordom iz baze 
                    {
                        MessageBox.Show("Šifra je tačna");
                        checkPassword = true;
                    }

                }
            }

            if (id != 0 && checkPassword) //ako je nađen ID i Password, otvori formu sa kontaktima i prosledi parametre
            {
                frmKontakti f = new frmKontakti();
                f.userId = id;
                f.ds = ds;
                f.da = da;
                f.conn = conn;
                f.Show();
            }
         
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            frmRegistracija f = new frmRegistracija();
            f.ds = ds;
            f.da = da;
            f.conn = conn;
            f.ShowDialog();

        }

        private void txtUsername_TextChanged(object sender, EventArgs e)
        {
            DataTable tbl = ds.Tables[0];
            foreach (DataRow slog in tbl.Rows)
            {
                if (txtUsername.Text == slog["username"].ToString()) //Ako se podataka iz txtUsername slaže sa podatkom iz baze
                {
                    pictureBox1.ImageLocation = slog["Slika"].ToString();
                }
                
            }
            

        }


    }
}
